package by.gravity.pvh.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import by.gravity.pvh.data.helper.SQLConstants;
import by.gravity.pvh.data.helper.SQLDataManagerHelper;
import by.gravity.pvh.model.BalconyBlockModel;
import by.gravity.pvh.model.BalconyRamaModel;
import by.gravity.pvh.model.CommonModel;
import by.gravity.pvh.model.DoorModel;
import by.gravity.pvh.model.WindowModel;

import com.google.gson.Gson;

public class SQLDataManager {

	private SQLDataManagerHelper sqlHelper;
	private SQLiteDatabase database;

	private static SQLDataManager instance;

	private SQLDataManager(Context context) {

		sqlHelper = new SQLDataManagerHelper(context);
		database = sqlHelper.getWritableDatabase();
	}

	public static SQLDataManager getInstance(Context context) {

		if (instance == null) {
			instance = new SQLDataManager(context);
		}
		return instance;
	}

	public interface onLoadCompleteListener {
		public void onLoadComplete(List<String> resutl);
	}

	public void getNames(final onLoadCompleteListener loadCompleteListener) {
		new AsyncTask<Void, Void, List<String>>() {

			@Override
			protected List<String> doInBackground(Void... params) {
				List<String> list = new ArrayList<String>();
				Cursor cursor = database.query(SQLConstants.TABLE_ORDER, new String[] { SQLConstants.FIELD_NAME }, null, null, null, null,
						SQLConstants.FIELD_TIME + " DESC");

				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					list.add(cursor.getString(cursor.getColumnIndex(SQLConstants.FIELD_NAME)));
				}

				cursor.close();

				return list;

			}

			@Override
			protected void onPostExecute(List<String> result) {
				super.onPostExecute(result);
				loadCompleteListener.onLoadComplete(result);
			}

		}.execute();

	}

	public enum TypeOrder {
		WINDOW, DOOR, BALCONY_BLOOCK, BALCONY_RAMA;
	}

	public void addOrder(final String name, final String order, final TypeOrder typeOrder) {
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				Cursor cursor = database.query(SQLConstants.TABLE_ORDER, null, SQLConstants.FIELD_NAME + "=?", new String[] { name }, null,
						null, null);

				ContentValues values = new ContentValues();
				if (cursor.moveToFirst()) {
					values.put(SQLConstants.FIELD_ID, cursor.getInt(cursor.getColumnIndex(SQLConstants.FIELD_ID)));
				}
				cursor.close();
				values.put(SQLConstants.FIELD_NAME, name);
				values.put(SQLConstants.FIELD_ORDER_DATA, order);
				values.put(SQLConstants.FIELD_TIME, String.valueOf(System.currentTimeMillis()));
				values.put(SQLConstants.FIELD_TYPE_ORDER, typeOrder.toString());

				database.insertWithOnConflict(SQLConstants.TABLE_ORDER, null, values, SQLiteDatabase.CONFLICT_REPLACE);

				return null;
			}
		}.execute();

	}

	public interface onLoadModelCompleteListener {
		public void onLoadComplete(CommonModel commonModel);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getOrder(final String nameOder, final onLoadModelCompleteListener listener) {
		new AsyncTask<Void, Void, CommonModel>() {

			@Override
			protected CommonModel doInBackground(Void... params) {
				Cursor cursor = database.query(SQLConstants.TABLE_ORDER, null, SQLConstants.FIELD_NAME + "=?", new String[] { nameOder },
						null, null, null);

				if (cursor.moveToFirst()) {
					CommonModel result = null;
					String typeOrderString = cursor.getString(cursor.getColumnIndex(SQLConstants.FIELD_TYPE_ORDER));
					TypeOrder typeOrder = TypeOrder.valueOf(typeOrderString);

					String orderData = cursor.getString(cursor.getColumnIndex(SQLConstants.FIELD_ORDER_DATA));
					Class gsonClass = null;
					switch (typeOrder) {
					case WINDOW:
						gsonClass = WindowModel.class;
						break;

					case DOOR:
						gsonClass = DoorModel.class;
						break;
					case BALCONY_BLOOCK:
						gsonClass = BalconyBlockModel.class;
						break;
					case BALCONY_RAMA:
						gsonClass = BalconyRamaModel.class;
						break;

					}
					result = new Gson().fromJson(orderData, gsonClass);
					cursor.close();

					return result;

				}

				return null;

			}

			@Override
			protected void onPostExecute(CommonModel result) {
				super.onPostExecute(result);

				listener.onLoadComplete(result);
			}

		}.execute();

	}

	public void deleteOrder(final String name) {
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				database.delete(SQLConstants.TABLE_ORDER, SQLConstants.FIELD_NAME + "=?", new String[] { name });
				return null;
			}

		}.execute();
	}
}
